layui.use(['form', 'jquery', 'layer', 'table', 'laydate'], function() {
	var form = layui.form;
	var $ = layui.$;
	var table = layui.table;
	var layer = layui.layer;
	var laydate = layui.laydate;
	var lay_filter = 'tpl_attr';
	// 表单渲染
	form.render('checkbox');
	form.render('select');
	
	send_info = function(string_uri, json_data, fun_callback) {
		layer.load(2);
		$.ajax({
			type: "post",
			url: string_uri,
			async: true,
			data: json_data,
			dataType: 'json',
			success: function(data) {
				layer.closeAll('loading');
				if (data['code'] == -101) {
					location.href = 'login.html';
					return false;
				} else if (data['code'] == -200) {
					layer.msg(data['msg'], {
						icon: 2,
						anim: 6
					});
					return false;
				} else if (data['code'] == 0 && data['msg'] == '参数错误') {
					layer.msg('参数错误', {
						icon: 2,
						anim: 6
					});
				} else {
					return fun_callback(data);
				}
			},
			error: function(xhr, type, errorThrown) {
				layer.msg('请求失败，访问地址：' + string_uri, {
					icon: 2,
					anim: 6
				});
				layer.closeAll('loading');
			}
		});
	};
	// 监听复选框
	form.on('checkbox(category_goods_alone)', function(data) {
		var check_name = $(this).attr('name');
		var value = data.elem.checked;
		if(value == true) {
			$('.goods-audit-div').hide();
		} else {
			$('.goods-audit-div').show();
		}
	})
	// 监听提交
	form.on('submit(submit_attr)', function(data) {
		var field = data.field;
		var url = $('form[lay-filter="' + lay_filter + '"]').attr('action');
		send_info(url, field, function(data) {
			if(data.code == 0) {
				layer.msg('保存成功', {
					icon: 1
				});
				// 表格重载
				window.parent.son_reload_table();
				// 关闭当前弹出层
				parent.layer.closeAll('iframe');
			} else {
				layer.msg(data.msg, {
					icon: 2,
					anim: 6
				});
			}
		})
		return false;
	})

})